package bigdata.backend.datasource.entity;


import com.baomidou.mybatisplus.annotation.*;
import bigdata.backend.utils.DbType;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Objects;

@Data
@TableName("datasource")
public class Datasource {
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;
    private String sourceName;
    private String host;
    private String username;
    private String password;
    private Integer port;
    private String defaultDatabase;
    private String schemaName; // postgresql 专用
    private String dbAlias; // 唯一别名 用于动态数据源 key
    private String dbType; // mysql/doris/postgresql
    private String deleteFlag = "0";

    // 添加自动填充注解
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    public String getEffectSchema() {
        DbType type = DbType.fromString(dbType);
        if (type == DbType.POSTGRESQL) {
            return Objects.toString(schemaName, "public");
        }
        return null;
    }
}
